package com.ipzoe.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.system.domain.Industry;
import com.ipzoe.system.service.IIndustryService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 行业Controller
 * 
 * @author eric
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/system/industry" )
public class IndustryController extends BaseController {

    @Autowired
    private IIndustryService iIndustryService;

    /**
     * 查询行业列表
     */
    @PreAuthorize("@ss.hasPermi('system:industry:list')")
    @GetMapping("/list")
    public TableDataInfo list(Industry industry)
    {
        startPage();
        LambdaQueryWrapper<Industry> lqw = new LambdaQueryWrapper<Industry>();
        if (StringUtils.isNotBlank(industry.getName())){
            lqw.like(Industry::getName ,industry.getName());
        }
        List<Industry> list = iIndustryService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出行业列表
     */
    @PreAuthorize("@ss.hasPermi('system:industry:export')" )
    @Log(title = "行业" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Industry industry) {
        LambdaQueryWrapper<Industry> lqw = new LambdaQueryWrapper<Industry>(industry);
        List<Industry> list = iIndustryService.list(lqw);
        ExcelUtil<Industry> util = new ExcelUtil<Industry>(Industry. class);
        return util.exportExcel(list, "industry" );
    }

    /**
     * 获取行业详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:industry:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iIndustryService.getById(id));
    }

    /**
     * 新增行业
     */
    @PreAuthorize("@ss.hasPermi('system:industry:add')" )
    @Log(title = "行业" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Industry industry) {
        industry.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iIndustryService.save(industry) ? 1 : 0);
    }

    /**
     * 修改行业
     */
    @PreAuthorize("@ss.hasPermi('system:industry:edit')" )
    @Log(title = "行业" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Industry industry) {
        industry.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iIndustryService.updateById(industry) ? 1 : 0);
    }

    /**
     * 删除行业
     */
    @PreAuthorize("@ss.hasPermi('system:industry:remove')" )
    @Log(title = "行业" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iIndustryService.removeByIds(Arrays.asList(ids), SecurityUtils.getUsername()) ? 1 : 0);
    }
}
